<?php

require_once './include/common.inc.php';
getMyParam('page');

if ($page == "links") {
    $page_title = 'jans2en furniture - Links';
    $print_title = 'Links';

    $sql = "SELECT `id`, `title`, `url` FROM `links` WHERE `publish_flag` = 1 ORDER BY `seq` ASC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $out_ary[] = $rs;
    }

    require_once PrintPrint('header');
    require_once PrintPrint('links');
    require_once PrintPrint('footer');
    exit;
} elseif ($page == "aboutus") {
    /*
    $page_title = 'jans2en furniture - About Us';
    $print_title = 'About Us';

    $aboutus = nl2br(getSystemValue("aboutus"));

    require_once PrintPrint('header');
    require_once PrintPrint('aboutus');
    require_once PrintPrint('footer');
    */
    exit;
} elseif ($page == "howtoorder") {
    /*
    $page_title = 'jans2en furniture - How to order';
    $print_title = 'How to order';

    require_once PrintPrint('header');
    if ($jansen_uid > 0 && $jansen_type > 0) {
    require_once PrintPrint('howtoorder2');
    } else {
    require_once PrintPrint('howtoorder');
    }
    require_once PrintPrint('footer');
    */
    exit;
} elseif ($page == "contactus") {
    $page_title = 'jans2en furniture - Contact Us';
    $print_title = 'Contact Us';

    getMyParam('firstname');
    getMyParam('lastname');
    getMyParam('contactaddress');
    getMyParam('contactcity');
    getMyParam('stateid');
    getMyParam('countryid');
    getMyParam('contactpostal');
    getMyParam('contactphone');
    getMyParam('contactemail');
    getMyParam('contactpreference');
    getMyParam('contactcomments');

    $s = '<tr><td align="right" width="30%"></td><td></td></tr>';
    if (trim($firstname != '')) {
        $s .= '<tr><td align="right">First Name:</td><td>&nbsp;' . $firstname . '</td></tr>';
    }
    if (trim($lastname != '')) {
        $s .= '<tr><td align="right">Last Name:</td><td>&nbsp;' . $lastname . '</td></tr>';
    }
    if (trim($contactaddress != '')) {
        $s .= '<tr><td align="right">Address:</td><td>&nbsp;' . $contactaddress . '</td></tr>';
    }
    if (trim($contactcity != '')) {
        $s .= '<tr><td align="right">City:</td><td>&nbsp;' . $contactcity . '</td></tr>';
    }
    if (trim($stateid > 0)) {
        $s .= '<tr><td align="right">Province/State:</td><td>&nbsp;' . $STATE_ARY[$stateid] . '</td></tr>';
    }
    if (trim($countryid > 0)) {
        $s .= '<tr><td align="right">Country:</td><td>&nbsp;' . $COUNTRY_ARY[$countryid] . '</td></tr>';
    }
    if (trim($contactpostal != '')) {
        $s .= '<tr><td align="right">Postal/Zip Code:</td><td>&nbsp;' . $contactpostal . '</td></tr>';
    }
    if (trim($contactphone != '')) {
        $s .= '<tr><td align="right">Phone:</td><td>&nbsp;' . $contactphone . '</td></tr>';
    }
    if (trim($contactemail != '')) {
        $s .= '<tr><td align="right">Email:</td><td>&nbsp;' . $contactemail . '</td></tr>';
    }
    if (trim($contactpreference != '')) {
        $s .= '<tr><td align="right">Preference:</td><td>&nbsp;' . $contactpreference . '</td></tr>';
    }
    if (trim($contactcomments != '')) {
        $s .= '<tr><td align="right">Comments:</td><td>&nbsp;' . $contactcomments . '</td></tr>';
    }

    require_once PrintPrint('header');
    require_once PrintPrint('contactus');
    require_once PrintPrint('footer');
    exit;
} elseif ($page == "distribution") {
    $page_title = 'jans2en furniture - Distribution';
    $print_title = 'Distribution';

    $sql = "SELECT * FROM `distribution` ORDER BY `seq` ASC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $out_ary[$rs['area']][] = $rs;
    }

    ksort($out_ary);
    $s = '';
    foreach ($out_ary as $k=>$v) {
        $s .= '<tr><td valign="top" colspan=2><strong>' . $AREA_ARY[$k]['name'] . '</strong></td><tr>';
        foreach ($v as $v1) {
            $s .= '<tr><td valign="top" width="40%">' . $v1['country'] . '</td><td valign="top" >' . $v1['type'] . '</td><tr>';
        }
        $s .= '<tr height=20><td colspan=2></td></tr>';
    }



    require_once PrintPrint('header');
    require_once PrintPrint('distribution');
    require_once PrintPrint('footer');
    exit;
} elseif ($page == "products") {
    /*
    $page_title = 'jans2en furniture - Products';
    $print_title = 'Products';

    $new_item_ary = array(
    'id' => '1001',
    'name' => 'New Items',
    'pic' => 'new_items.jpg',
    'seq' => '1001',
    'description' => 'At jans2en our goal is to continuously discover, manufacture, and bring to our customers the very best selection in fine reproduction furnishings and home accessories on a continual basis. Visit this section of our web site on a regular basis to disco'
    );

    $sql = "SELECT `id`, `name`, `pic`, `seq`, `description` FROM `category` WHERE `fid` = 0 ORDER BY `seq` ASC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
    $out_ary[] = $rs;
    }
    $out_ary[] = $new_item_ary;

    require_once PrintPrint('header');
    require_once PrintPrint('products');
    require_once PrintPrint('footer');
    */
    exit;
} elseif ($page == "product_details") {
    //checkLogin();
    getMyParam('pid');
    $page_title = 'jans2en furniture - Product Detail';
    $print_title = 'Product Detail';

    $sql = "SELECT * FROM `product` WHERE `id`='$pid' AND `hidden_flag`=0";
    $product_info_ary = $db->get_one($sql);

    $product_info_ary['width'] = sprintf("%0.2f", $product_info_ary['width']);
    $product_info_ary['height'] = sprintf("%0.2f", $product_info_ary['height']);
    $product_info_ary['length'] = sprintf("%0.2f", $product_info_ary['length']);

    $width_inch = sprintf("%0.2f", $product_info_ary['width2']);
    $height_inch = sprintf("%0.2f", $product_info_ary['height2']);
    $length_inch = sprintf("%0.2f", $product_info_ary['length2']);

    $product_info_ary['cbm'] = sprintf("%0.2f", $product_info_ary['cbm']);
    $product_info_ary['cuft'] = sprintf("%0.2f", $product_info_ary['cuft']);

    if (trim($product_info_ary['pic']) == "" || !file_exists("./zh/attachments/" . $product_info_ary['pic'])) {
        $product_info_ary['pic'] = 'no_big.gif';
    }
	else{
		$imgsize = getimagesize('./zh/attachments/'.$product_info_ary['pic']);
		if($imgsize[0] > 533){
			$product_info_ary['pic_width'] = ' width="533"';
		}
	}

    if ($jansen_type==2) {
        $money_type = 'EUR';
        $product_info_ary['price_2'] = number_format($product_info_ary['price_2'], 2, '.', ',');
        $price_show = $product_info_ary['price_2'];
    } elseif ($jansen_type == 1) {
        $money_type = 'EUR';
        $product_info_ary['price_3'] = number_format($product_info_ary['price_3'], 2, '.', ',');
        $price_show = $product_info_ary['price_3'] ;
    }

    //require_once PrintPrint('header');
    require_once PrintPrint('all_product_details');
    //require_once PrintPrint('footer');
    exit;
} elseif ($page == "inventory_category") {
    checkLogin();
    getMyParam('inventoryid');
    $page_title = 'jans2en furniture - Inventory';
    $print_title = 'Inventory';



    //delete_flag=1ĲƷʾ
    $delete_flag_str = '';

    $sql = "SELECT `id` FROM `product` WHERE `delete_flag` = 1";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $delete_flag_ary[] = $rs['id'];   //ΪɾĲƷ
    }

    if (!empty($delete_flag_ary)) {
        $delete_flag_str = " AND c.`product_id` NOT IN (" . implode(",", $delete_flag_ary) . ") ";
    }


    // ===== ֻʾвƷûвƷͲʾ start ======
    // inventory  categoryһ
	//$sql = "SELECT c.`category_id`, i.`inventory_id` FROM `product_inventory` i, `product_category` c WHERE i.`product_id`=c.`product_id` AND c.`seq`>0 AND i.`qty`>0 " . $sql_2 . " AND i.`inventory_id` = $inventoryid" . $delete_flag_str;
	$sql = "SELECT c.`category_id`, i.`inventory_id` FROM `product_inventory` i, `product_category` c WHERE i.`product_id`=c.`product_id` AND i.`qty`>0 " . $sql_2 . " AND i.`inventory_id` = $inventoryid" . $delete_flag_str;
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        //$category_id_ary[$rs['inventory_id']][] = $rs['category_id'];
        $category_id_ary[] = $rs['category_id'];
    }

    $category_id_ary = @array_unique($category_id_ary);
    $category_id_str = @implode(",", $category_id_ary);

    if (!empty($category_id_ary)) {
        $sql = "SELECT `id`, `name`, `pic`, `seq`, `fid`, `description` FROM `category` WHERE `fid`>0 AND `id` IN (" . $category_id_str . ")";
        $query = $db->query($sql);
        while($rs = $db->fetch_array($query)) {
            $category_id_ary[] = $rs['fid'];
        }
        $category_id_ary = array_unique($category_id_ary);
        $category_id_str = implode(",", $category_id_ary);
    }

    // === end ========================

    if (!empty($category_id_ary)) {
        $sql = "SELECT `id`, `name`, `pic`, `seq`, `fid`, `description` FROM `category` WHERE `fid` = 0 AND `id` IN (" . $category_id_str . ") ORDER BY `seq` ASC";
        $query = $db->query($sql);
        while($rs = $db->fetch_array($query)) {
            $out_ary[$rs['id']] = $rs;
        }
    } else {
        $out_ary = array();
    }

    require_once PrintPrint('header');
    require_once PrintPrint('inventory');
    require_once PrintPrint('footer');
    exit;
} elseif ($page == "cart_submit") {
    checkLogin();

    getMyParam('orderid');
    $sql = "SELECT * FROM `order` WHERE `id` = '$orderid'";
    $order_info = $db->get_one($sql);

    if ($jansen_uid != $order_info['user_id']) {
        header("Location:index.php");
        exit;
    }
    $tmp_ary = explode("{}", $order_info['product_info']);
    $info_ary = array();
    if (!empty($tmp_ary)) {
        foreach ($tmp_ary as $v) {
            @list($type, $pid, $count, $price, $name,$fitemid) = explode("|", $v);
            $cost = $count*$price;
			$info_ary[$type . '_' . $pid.'_'.$fitemid] = array('name'=>$name, 'price'=>$price, 'count'=>$count, 'intype'=>$type, 'cost'=>$cost, 'fitemid'=>$fitemid);
        }
    }
    $page_title = 'jans2en furniture - Order - Confirmation';
    $print_title = 'Confirmation';
    //echo "<pre>";print_r($order_info);print_r($info_ary);exit;
    $total_qty = $order_info['totalcount'];
    $total_cost = $order_info['totalcost'];

    $all_product_info_ary = array();
	foreach ($info_ary as $pid=>$vv) {
        @list($type, $pid,$fitemid) = explode("_", $pid);
		$pid_ary[] = $pid;
    }
    if (!empty($pid_ary)) {
        $pid_str = implode(",", $pid_ary);
        $sql = "SELECT `id`, `code`, `small_pic` FROM `product` WHERE id IN (" . $pid_str . ")";
        $query = $db->query($sql);
        while($rs = $db->fetch_array($query)) {
            $all_product_info_ary[$rs['id']] = $rs;
        }
    }

    if ($jansen_type == 1) {
        $money_type = " EUR";
    } else {
        $money_type = " EUR";
    }

    $sql = "SELECT * FROM `user` WHERE `id`='$jansen_uid'";
    $user_info_ary = $db->get_one($sql);

    $sql = "SELECT * FROM `inventory`";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $location_info_ary[$rs['id']] = $rs['name'];
    }

	require_once PrintPrint('header');
    require_once PrintPrint('cart_submit');
    require_once PrintPrint('footer');
    exit;
} elseif ($page == "disclaimer") {
    $page_title = 'jans2en furniture - Disclaimer';
    $print_title = 'Disclaimer';

    require_once PrintPrint('header');
    require_once PrintPrint('disclaimer');
    require_once PrintPrint('footer');
    exit;
} elseif ($page == "privacy") {
    $page_title = 'jans2en furniture - Privacy';
    $print_title = 'Privacy';

    require_once PrintPrint('header');
    require_once PrintPrint('privacy');
    require_once PrintPrint('footer');
    exit;
} elseif ($page == "cart_view") {
    $page_title = 'jans2en furniture - Order';
    $print_title = 'Order';

    checkLogin();

    if ($jansen_type == 1) {
        $money_type = " EUR";
    } else {
        $money_type = " EUR";
    }

	$info_ary = $JANSEN_CONTAINER_ARY;
	ksort($info_ary);
    $total_qty = $JANSEN_CONTAINER_TOTAL_QTY;
    $total_cost = $JANSEN_CONTAINER_TOTAL_COST;

    $all_product_info_ary = array();
    foreach ($info_ary as $pid=>$vv) {
        $tpm_ary = explode("_", $pid);
        //$pid = array_pop($tpm_ary);
		$pid = $tpm_ary[1];
        $pid_ary[] = $pid;
    }

    if (!empty($pid_ary)) {
        $pid_str = implode(",", $pid_ary);

		$sql = "SELECT `id`, `code`, `small_pic`, `pic`, `cbm`, `cuft`, `finish_id` FROM `product` WHERE id IN (" . $pid_str . ")";
        $query = $db->query($sql);
        while($rs = $db->fetch_array($query)) {
            $all_product_info_ary[$rs['id']] = $rs;
        }
    }

    $sql = "SELECT * FROM `user` WHERE `id`='$jansen_uid'";
    $user_info_ary = $db->get_one($sql);
    require_once PrintPrint('header');
    require_once PrintPrint('cart_view');
    require_once PrintPrint('footer');
    exit;
} elseif ($page == "tradeshows_list") {
    $page_title = 'jans2en furniture - Tradeshows - List';
    $print_title = 'Tradeshows';

    $sql = "SELECT `id`, `date`, `show`, `location`, `country`, `booth`, `company` FROM `tradeshows` ORDER BY `seq` ASC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $out_ary[] = $rs;
    }

    require_once PrintPrint('header');
    require_once PrintPrint('tradeshows_list');
    require_once PrintPrint('footer');
    exit;
} elseif ($page == "tradeshows_detail") {
    getMyParam('tid');
    $page_title = 'jans2en furniture - Tradeshows - Details';
    $print_title = 'Tradeshows';

    $photo_category_ary = array();
    $sql = "SELECT * FROM `tradeshows` WHERE `id` = $tid";
    $tradeshows_details_ary = $db->get_one($sql);

    require_once PrintPrint('header');
    require_once PrintPrint('tradeshows_detail');
    require_once PrintPrint('footer');
    exit;
}

header("Location:index.php");
?>